<template>
  <avue-crud
    ref="messageRef"
    :table-loading="tableLoading"
    v-model="form"
    :option="option"
    @refresh-change="getList"
    @row-save="rowSave"
    @row-update="rowUpdate"
    @row-del="rowDel"
    :data="data"
  ></avue-crud>
</template>

<script setup>
import { ref, reactive, getCurrentInstance, onMounted } from "vue";
import message from "@/options/message";
import { ElMessageBox, ElMessage } from "element-plus";
import { getMsg, addMsg, editMsg, delMsg } from "@/apis/admin";

onMounted(() => {
  getList();
});
const getList = async () => {
  tableLoading.value = true;
  const res = await getMsg.get("", {});
  tableLoading.value = false;
  if (res.code == 200) data.value = res.data.list;
};
let { proxy } = getCurrentInstance();
const option = message;
const data = ref([]);
const form = ref({});
const tableLoading = ref(false);

const rowSave = async (row, done, loading) => {
  const res = await addMsg.post("", row);
  if (res.code == 200) {
    done();
    ElMessage.success("添加成功!");
    getList();
  } else {
    ElMessage.error("添加失败!");
  }
};
const rowDel = (row, index, done) => {
  console.log(row);
  ElMessageBox.confirm("此操作将永久删除该消息, 是否继续?", "提示", {
    confirmButtonText: "确认",
    cancelButtonText: "取消",
    type: "warning",
  }).then(() => {
    delMsg.post("", { id: row.id }).then((res) => {
      if (res.code == 200) {
        ElMessage.success("删除成功!");
        getList();
      } else {
        ElMessage.error(res.msg);
      }
    });
  });
};
const rowUpdate = async (row, index, done, loading) => {
  const res = await editMsg.post("", row);
  if (res.code == 200) {
    done();
    ElMessage.success("编辑成功!");
    getList();
  } else {
    ElMessage.error("编辑失败!");
  }
};
</script>
